// true为校验通过  false为不通过

// 校验姓名
const checkInfoName=(name: string)=>{
  if (/^([\u2E80-\uFE4F]{1,10}|[a-zA-Z\.\s]{1,40})$/.test(name)) {
      return true;
  }else{
      return false;
  }
}

//校验护照，港澳台通行证的证件号
const checkInfoCard=(type: string,name: string)=>{
  //type 2护照   6港澳台通行证
  if(type=="2" && (name.length<3 || name.length>10)){
      return false;
  }else if(name.length<5){
      return false;
  }
  return true;
}

// 校验手机号 
const checkInfoTel=(tel: string)=>{
  if (/^[1][0-9]{10}$/.test(tel)) {
    return true;
  }
  return false;
}

//身份证号校验
const verIdCard=(arrObj: string)=>{
  // 判断身份证号是否符合规则
  if(!/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(arrObj)){
    return false;
  }
  // 身份证
  // 加权因子
  var weight_factor = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,];
  var check_code = ["1","0","X","9","8","7","6","5","4","3","2",];
  var code = arrObj + "";
  var last = arrObj[17]; //最后一位
  var seventeen = code.substring(0, 17);
  // 判断最后一位校验码是否正确
  var arr:Array<string> = seventeen.split("");
  var len = arr.length;
  var num = 0;
  for (var i = 0; i < len; i++) {
    num = num + Number(arr[i]) * weight_factor[i];
  }
  // 获取余数
  var resisue = num % 11;
  var last_no = check_code[resisue];
  return last_no==last;
}

// 获取用户年龄
const userAge=(str: string)=>{
  // 格式2010-01-10
  var birthYear = str.split("-")[0];
  var birthMonth = str.split("-")[1];
  var birthDay = str.split("-")[2];
  var d = new Date();
  var nowYear = d.getFullYear();
  var nowMonth = d.getMonth() + 1;
  var nowDay = d.getDate();
  var returnAge = 0; // 年龄
  var ageDiff = nowYear - Number(birthYear); //年之差
  var dayDiff = nowDay - Number(birthDay); //日之差
  if (ageDiff == 0) {
    returnAge = 0; //同年 则为0岁
  } else if (ageDiff > 0) {
    if (nowMonth == Number(birthMonth)) {
      if (dayDiff < 0) {
        returnAge = ageDiff - 1;
      } else {
        returnAge = ageDiff;
      }
    } else {
      var monthDiff = nowMonth - Number(birthMonth); //月之差
      if (monthDiff < 0) {
        returnAge = ageDiff - 1;
      } else {
        returnAge = ageDiff;
      }
    }
  }
  return returnAge;
}

const formatDate=(date: string)=>{
  const d = new Date(date);
	const Y = d.getFullYear() + '-';
	const M = (d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1) + '-';
	const D = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
	// let h = (d.getHours() < 10 ? '0' + d.getHours() : d.getHours()) + ':';  // 小时
	// let m = (d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()) + ':'; // 分
	// let s = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds() // 秒
    // let datetime = `${Y}${M}${D} ${h}${m}${s}`
    let datetime = `${Y}${M}${D}`
	return datetime;
}
export {
  checkInfoName,//校验姓名
  checkInfoCard,//校验护照等证件
  checkInfoTel,//校验手机号
  verIdCard,//校验身份证
  userAge,//传入生日日期，换取年龄
  formatDate,//时间格式转换
}